package com.echoesnet.echoesshortvideo;

import android.app.Activity;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.baidu.mapapi.SDKInitializer;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.CrashHandler;
import com.echoesnet.echoesshortvideo.Utils.Log.EamLogger;
import com.echoesnet.echoesshortvideo.Utils.VideoUtils;
import com.google.gson.Gson;
import com.mob.MobSDK;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 *
 * @author ben
 * @version 1.0
 * @modifier
 * @createDate 2018/4/20 16:38
 * @description
 */
public class AppController extends MultiDexApplication //implements HasActivityInjector
{
    private static AppController appInstance;
    private static Gson gson;
    private List<Activity> activityList = new ArrayList<>();

    public int videoPlayPagePosition = 0;
    public boolean videoRefuseNetTip = false;

//    @Inject
//    DispatchingAndroidInjector<Activity> activityInjector;

    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        appInstance = this;
        String processName = CommonUtils.getProcessName(this, android.os.Process.myPid());
        if (processName != null)
        {
            boolean defaultProcess = processName.equals(BuildConfig.APPLICATION_ID);
            if (defaultProcess)
            {
                switchAppEnvironment(BuildConfig.evnIndex);

                LogLevel logLevel=LogLevel.NONE;
                com.echoesnet.echoesshortvideo.Utils.Log.LogLevel eamLogLevel=com.echoesnet.echoesshortvideo.Utils.Log.LogLevel.NONE;

                if(BuildConfig.enableDebugLog)
                {

                     logLevel=LogLevel.FULL;
                     eamLogLevel=com.echoesnet.echoesshortvideo.Utils.Log.LogLevel.FULL;
                }
                Logger.init("EchoesShortVideo")
                        .logLevel(logLevel);

                EamLogger.init("EchoesShortVideo")
                        .logLevel(eamLogLevel);
                //AppInjector.init(appInstance);
                MobSDK.init(this);

                //崩溃捕捉
                CrashHandler.getInstance().init(getApplicationContext());
            }
        }
        //百度定位初始化
        SDKInitializer.initialize(getApplicationContext());
    }

//    @Override
//    public AndroidInjector<Activity> activityInjector()
//    {
//        return activityInjector;
//    }

    /**
     * 切换app的执行环境
     *
     * @param envType 1：公网正式环境 2：公网测试环境 3：内侧环境 4.昕琰ip、测试直播接口
     */
    private void switchAppEnvironment(int envType)
    {
        switch (envType)
        {
            case 1:
                Api.SERVER_SITE = "http://120.132.92.73:8855";
                Api.SHARE_URL = "http://fx.echoesnet.com/share/index.html";
                break;
            case 2:
                Api.SERVER_SITE = "http://120.132.92.73:8855";
                Api.SHARE_URL = "http://fxcs.echoesnet.com/share/index.html";
                break;
        }
    }


    public static AppController getInstance()
    {
        return appInstance;
    }

    //添加activity到容器中
    public void addActivity(Activity activity)
    {
        activityList.add(activity);
    }

    //遍历所有的Activiy并finish
    public void clearTopActivity()
    {
        for (Activity activity : activityList)
        {
            activity.finish();
        }
        activityList.clear();
    }

    public static Gson getGsonInstance()
    {
        if (gson == null)
            gson = new Gson();
        return gson;
    }
}
